package cn.csxw.openchina.ui.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.csxw.openchina.R;
import cn.csxw.openchina.base.BaseFragment;
import cn.csxw.openchina.ui.activity.CircleFriendsActivity;
import cn.csxw.openchina.ui.activity.ExerciseActivity;
import cn.csxw.openchina.ui.activity.ScanActivity;
import cn.csxw.openchina.ui.activity.ScanActivity1;
import cn.csxw.openchina.ui.activity.ShakeActivity;
import cn.csxw.openchina.utils.Global;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;

/**
 * 查找发现功能
 */
public class DiscoverFragment extends BaseFragment implements EasyPermissions.PermissionCallbacks {

    @BindView(R.id.tv_friends)
    TextView mTvFriends;
    @BindView(R.id.tv_findPeople)
    TextView mTvFindPeople;
    @BindView(R.id.tv_exercise)
    TextView mTvActivity;
    @BindView(R.id.tv_RichScan)
    TextView mTvRichScan;
    @BindView(R.id.tv_shake)
    TextView mTvShake;

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_discover;
    }

    @Override
    public void initView() {

    }


    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v, int id) {
        switch (id) {
            case R.id.tv_friends:
                circleFriends();
                break;
            case R.id.tv_findPeople:
                break;
            case R.id.tv_exercise:
                exercise();
                break;
            case R.id.tv_RichScan:
                enterScan();//扫一扫
                break;
            case R.id.tv_shake:
                shakeShake();
                break;
        }

    }

    // 活动界面中的跳转到摇一摇界面
    private void shakeShake() {
        Intent intent = new Intent(mActivity, ShakeActivity.class);
        startActivity(intent);
    }


    //活动界面中的跳转到朋友圈界面
    private void circleFriends() {
        Intent intent = new Intent(mActivity, CircleFriendsActivity.class);
        startActivity(intent);
    }

    // 活动界面中的跳转到近期活动和我的活动
    private void exercise() {
        Intent intent = new Intent(mActivity, ExerciseActivity.class);
        startActivity(intent);
    }
    private static final int REQ_EXT =666;
    ///   扫一扫权限相关
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        //成功
        scan();
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
    //失败
    }

    // 活动界面中的跳转到扫一扫界面  要将扫描后的结果返回回来
    private void enterScan() {
        /*Intent intent = new Intent(mActivity, ScanActivity1.class);
        startActivity(intent);*/
        if(Build.VERSION.SDK_INT>=23){
            loadCameraPermission();
        }else{
            scan();
        }

        //打开扫一扫功能
    }

    //请求权限之后再去调用
    @AfterPermissionGranted(value = REQ_EXT)
    public void loadCameraPermission (){
        String []permission = {Manifest.permission.CAMERA};
        boolean isHas = EasyPermissions.hasPermissions(mActivity,permission);
        if(isHas){
            scan();
        }else{
            //没有就要去请求权限
            EasyPermissions.requestPermissions(this,"请求摄像头权限扫一扫", REQ_EXT,permission);
        }
    }

    //按钮点击
    public void scan(){
        if (mActivity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        IntentIntegrator intentIntegrator =
                new IntentIntegrator(mActivity);
        intentIntegrator.initiateScan();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //请求权限
        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        IntentResult intentResult =
                IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            String result = intentResult.getContents();

            Global.showToast("扫描成功  ：" + result  );
        } else {
            Global.showToast("扫描失败");
        }
    }
}
